package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.MemberEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.dao.TopicArticleLabelsTDao;
import com.jshoperxms.dao.TopicArticleTDao;
import com.jshoperxms.entity.TopicArticleLabelsT;
import com.jshoperxms.entity.TopicArticleT;
import com.jshoperxms.service.TopicArticleTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/11/22.
 * Des:
 */
@Service("topicArticleTService")
@Scope("prototype")
public class TopicArticleTServiceImpl extends BaseTServiceImpl<TopicArticleT> implements TopicArticleTService {


    @Resource
    private TopicArticleTDao topicArticleTDao;

    @Resource
    private TopicArticleLabelsTDao topicArticleLabelsTDao;
    @Resource
    private Serial serial;



    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void saveTopicArticleTransactional(TopicArticleT rq) {
        //1,保存话题文章
        TopicArticleT bean=new TopicArticleT();
        bean.setId(this.serial.Serialid(SerialKeys.TOPICARTICLET));
        bean.setAuthor(rq.getAuthor());
        bean.setCai(0);
        bean.setContentvalue(rq.getContentvalue());
        bean.setCreatorid(BaseTools.getAdminCreateId());
        bean.setCreatetime(DateTools.getSystemTimeBi());
        bean.setHtmlpath(StaticKey.EMPTY);
        bean.setIsrecommend(rq.getIsrecommend());
        bean.setIstop(rq.getIstop());
        Map<String,String> map=BaseTools.parseImg(rq.getMainimg());
        bean.setMainimg(map.get(TagWords.NORMALFILEPATH));
        bean.setSmainimg(map.get(TagWords.COMPRESSFILEPATH));
        bean.setMetades(rq.getMetades());
        bean.setMetakey(rq.getMetakey());
        bean.setMsync(rq.getMsync());
        bean.setReadcount(0);
        bean.setReplycount(0);
        bean.setReviewed(MemberEnums.MemberWithdrawalsReviewed.PASS.getState());
        bean.setSign(SerialKeys.TOPICARTICLET+TagWords.HR+DateTools.getSystemTimeBi());
        bean.setSort(rq.getSort());
        bean.setStatus(rq.getStatus());
        bean.setTipcontent(rq.getTipcontent());
        bean.setTopicid(rq.getTopicid());
        bean.setTopicname(rq.getTopicname());
        bean.setTopiclabels(rq.getTopiclabels());
        bean.setTopictitle(rq.getTopictitle());
        bean.setUpdatetime(DateTools.getSystemTimeBi());
        bean.setVersiont(1);
        bean.setZan(0);
        bean.setReviewedtime(BigInteger.valueOf(1L));
        this.topicArticleTDao.save(bean);
        String[] ids= StringUtils.split(rq.getTopiclabels(), TagWords.SPLITDOT);
        for(String s:ids){
            TopicArticleLabelsT b=new TopicArticleLabelsT();
            b.setId(this.serial.Serialid(SerialKeys.TOPICARTICLELABELST));
            b.setCreatetime(DateTools.getSystemTimeBi());
            b.setName(s);
            b.setStatus(BaseEnums.DataUsingState.USING.getState());
            b.setTopicarticleid(bean.getId());
            b.setUpdatetime(DateTools.getSystemTimeBi());
            b.setVersiont(1);
            this.topicArticleLabelsTDao.save(b);
        }
    }



    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void updateTopicArticleTransactional(TopicArticleT rq) {
        //1,更新话题文章
        TopicArticleT bean=this.topicArticleTDao.findByPK(TopicArticleT.class,rq.getId());
        if(bean!=null){
            bean.setAuthor(rq.getAuthor());
            bean.setContentvalue(rq.getContentvalue());
            bean.setIsrecommend(rq.getIsrecommend());
            bean.setIstop(rq.getIstop());
            Map<String,String> map=BaseTools.parseImg(rq.getMainimg());
            bean.setMainimg(map.get(TagWords.NORMALFILEPATH));
            bean.setSmainimg(map.get(TagWords.COMPRESSFILEPATH));
            bean.setMetades(rq.getMetades());
            bean.setMetakey(rq.getMetakey());
            bean.setMsync(rq.getMsync());
            bean.setSort(rq.getSort());
            bean.setStatus(rq.getStatus());
            bean.setTipcontent(rq.getTipcontent());
            bean.setTopicid(rq.getTopicid());
            bean.setTopicname(rq.getTopicname());
            bean.setTopiclabels(rq.getTopiclabels());
            bean.setTopictitle(rq.getTopictitle());
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setVersiont(bean.getVersiont()+1);
            this.topicArticleTDao.update(bean);
        }
        //2,删除原标签记录
        Criterion criterion= Restrictions.eq("topicarticleid",rq.getId());
        List<TopicArticleLabelsT> beanlist=this.topicArticleLabelsTDao.findByCriteria(TopicArticleLabelsT.class,criterion);
        if(!beanlist.isEmpty()){
            this.topicArticleLabelsTDao.deleteAll(beanlist);
        }
        //3,更新标签表
        String[] ids= StringUtils.split(rq.getTopiclabels(), TagWords.SPLITDOT);
        for(String s:ids){
            TopicArticleLabelsT b=new TopicArticleLabelsT();
            b.setId(this.serial.Serialid(SerialKeys.TOPICARTICLELABELST));
            b.setCreatetime(DateTools.getSystemTimeBi());
            b.setName(s);
            b.setStatus(BaseEnums.DataUsingState.USING.getState());
            b.setTopicarticleid(bean.getId());
            b.setUpdatetime(DateTools.getSystemTimeBi());
            b.setVersiont(1);
            this.topicArticleLabelsTDao.save(b);
        }
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void delTopicArticleTransactional(TopicArticleT rq) {
        String []tids=StringUtils.split(rq.getId(), TagWords.SPLITDOT);
        for(String s:tids){
            TopicArticleT bean=this.topicArticleTDao.findByPK(TopicArticleT.class,s);
            if(bean!=null){
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                this.topicArticleTDao.update(bean);
                Criterion criterion= Restrictions.eq("topicarticleid",bean.getId());
                List<TopicArticleLabelsT> beanlist=this.topicArticleLabelsTDao.findByCriteria(TopicArticleLabelsT.class,criterion);
                if(!beanlist.isEmpty()){
                    this.topicArticleLabelsTDao.deleteAll(beanlist);
                }
            }


        }

    }
}
